package com.baobao.action;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
import net.sf.json.util.PropertyFilter;

import com.baobao.domain.Askleave;
import com.baobao.domain.Bbevent;
import com.baobao.domain.Notice;
import com.baobao.domain.School;
import com.baobao.service.AskleaveService;
import com.baobao.service.BbeventService;
import com.baobao.service.NoticeService;
import com.baobao.service.SchoolService;
import com.baobao.service.StudentService;
import com.baobao.utility.*;

@Controller
@RequestMapping("/leave")
public class LeaveAction {

	@Autowired
	private AskleaveService leaveService;
	
	@Autowired
	private StudentService studentService;
	
	private static Logger log = LoggerFactory.getLogger(LeaveAction.class);
	/**
	 * 提交请假申请
	 */
	@RequestMapping(value="/apply",produces = "application/json; charset=utf-8",method = {RequestMethod.GET})
	@ResponseBody
	public String AddNotice(HttpServletRequest request,
		@RequestParam(value="parentPhone",required=false) String parentPhone,
		@RequestParam(value="studentId",required=false) String studentId,
		@RequestParam(value="applyTime",required=false) String applyTime,
		@RequestParam(value="reason",required=false) String reason,
		@RequestParam(value="startTime",required=false) String startTime,
		@RequestParam(value="endTime",required=false) String endTime,
		@RequestParam(value="detail",required=false) String detail,
		@RequestParam(value="approver",required=false) String approver){
		
		log.info("收到学生请假申请==>parentPhone："+parentPhone+";studentId:"+studentId+";applyTime:"+applyTime+";reason:"+reason+";startTime:"+startTime
				+";endTime:"+endTime+";detail:"+detail+";approver:"+approver);
		
		
		SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm");
		
		Askleave leave = new Askleave();
		leave.setParentPhone(parentPhone);
		leave.setStudentId(studentId);
		leave.setReason(reason);
		
		try {
			Date apply_time =sdf.parse(applyTime);
			Calendar calendar_applytime = Calendar.getInstance();
			calendar_applytime.setTime(apply_time);
			leave.setApplyTime(calendar_applytime);
			
			Date start_time =sdf.parse(startTime);
			Calendar calendar_starttime = Calendar.getInstance();
			calendar_starttime.setTime(start_time);
			leave.setStartTime(calendar_starttime);
			
			Date end_time =sdf.parse(endTime);
			Calendar calendar_endtime = Calendar.getInstance();
			calendar_endtime.setTime(end_time);
			leave.setEndTime(calendar_endtime);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	
		leave.setDetail(detail);
		leave.setTeacherPhone(approver);
		leave.setApproveTime(Calendar.getInstance());
		leaveService.saveAskleave(leave);
		
		log.info("成功将请假申请写入数据库==>"+leave.toString());
		
		JSONObject result = new JSONObject();
		result.put("success", true);
		return result.toString();
	}
	
	/**
	 * 获取请假列表列表
	 */
	@RequestMapping(value="/get",produces = "application/json; charset=utf-8",method = {RequestMethod.GET})
	@ResponseBody
	public String GetLeaveInfo(HttpServletRequest request,
			@RequestParam(value="parentPhone",required=false) String parentPhone){
		
		log.info("收到获取请假列表命令。parentPhone："+parentPhone);
		JSONObject result = new JSONObject();
		
				
		//从数据库中读取leave
		Set<Askleave> leaves = leaveService.loadAskleaves();
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Calendar.class,new JsonCalendarValueProcessor("yyyy-MM-dd HH:mm"));
		JSONArray leaveList = JSONArray.fromObject(leaves,jsonConfig);	
		for (int i = 0; i < leaveList.size(); i++) {  
			JSONObject jsonObj = leaveList.getJSONObject(i);
			String studentId = (String)jsonObj.get("studentId");
			String studentName = studentService.findStudentByPrimaryKey(studentId).getName();
			jsonObj.put("studentName", studentName);
		}
				
		result.put("success", true);
		result.put("message", leaveList);
		log.info("返回的请假列表为："+result.toString());
		return result.toString();
	}

}
